/*
    This file is part of Manhunt.

    Manhunt is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Manhunt is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Manhunt.  If not, see <https://www.gnu.org/licenses/>.
 */

package snw.manhunt.managers;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;
import snw.manhunt.Manhunt;
import snw.manhunt.tasks.HunterWaitTimeOut;

import java.util.ArrayList;
import java.util.Objects;

public final class GameManager {
    private static boolean IS_GAME_STARTED = false;
    private static boolean HUNTER_SET_OUT = false;
    private static String RUNNER = null;
    private static final ArrayList<String> HUNTER = new ArrayList<>();
    private static HunterWaitTimeOut TIME_OUT_OBJ = null;

    public static void startGame() {
        if (ConfigManager.getHunterWaitTime() != 0) {
            TIME_OUT_OBJ = new HunterWaitTimeOut();
            TIME_OUT_OBJ.runTaskTimer(Manhunt.getInstance(), 0L, 20L);
        } else {
            for (String o : getHunters()) {
                Player i = Bukkit.getPlayerExact(o);
                if (i == null) {
                    getHunters().remove(o);
                    continue;
                }
                i.sendTitle(ChatColor.DARK_RED + "" + "GO!!!", "", 0, 70, 0);
            }
            GameManager.setHunterSetOut(true);
        }

        if (getRunner() == null) {
            GameManager.stopGame();
            return;
        }

        getRunner().sendTitle(ChatColor.GREEN + "" + "RUN!", "", 0, 70, 0);

        IS_GAME_STARTED = true;
    }

    public static boolean isGameStarted() {
        return IS_GAME_STARTED;
    }

    @Nullable
    public static Player getRunner() {
        return Bukkit.getPlayerExact(RUNNER);
    }

    public static void setRunner(Player runner) {
        if (runner == null) {
            RUNNER = null;
            return;
        }
        RUNNER = runner.getName();
    }

    public static ArrayList<String> getHunters() {
        return HUNTER;
    }

    public static void setHunterSetOut(boolean bool) {
        HUNTER_SET_OUT = bool;
    }

    public static boolean isHunterSetOut() {
        return HUNTER_SET_OUT;
    }

    public static void stopGame() {
        for (Player i : Bukkit.getOnlinePlayers()) {
            i.setGameMode(GameMode.SURVIVAL);
        }
        IS_GAME_STARTED = false;
        TIME_OUT_OBJ.cancel();
        TIME_OUT_OBJ = null;
        RUNNER = null;
        HUNTER.clear();
    }

    public static boolean removeIfNotOnline(String player) {
        if (Bukkit.getPlayerExact(player) == null) {
            getHunters().remove(player);
            if (Objects.equals(RUNNER, player)) RUNNER = null;
            return true;
        }
        return false;
    }
}
